package local.blueheart.data.springdatademo.controller;

import local.blueheart.data.springdatademo.domain.task.TaskEntity;
import local.blueheart.data.springdatademo.domain.task.TaskWorkerVO;
import local.blueheart.data.springdatademo.exception.DataTransferException;
import local.blueheart.data.springdatademo.service.TaskService;
import local.blueheart.data.springdatademo.service.TaskWorkerService;
import local.blueheart.data.springdatademo.util.PageUtils;
import local.blueheart.data.springdatademo.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/task")
public class TaskController  extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TaskController.class);

    @Autowired
    private TaskService taskService;

    @Autowired
    private TaskWorkerService taskWorkerService;

    @GetMapping("/task.do")
    public String taskIndex() {
        return "task/task";
    }

    @GetMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> params){
        int page = Integer.parseInt(params.get("offset").toString());
        int size = Integer.parseInt(params.get("limit").toString());
        Page<TaskEntity> taskEntityPage = taskService.list(page,size);
        return new PageUtils(taskEntityPage.getContent(), (int) taskEntityPage.getTotalElements());
    }

    /**
     * 增加视图
     * @return
     */
    @GetMapping("/add.do")
    public String add(Model model){
        return "/task/add";
    }

    @GetMapping("/taskworkerlist")
    @ResponseBody
    public List<TaskWorkerVO> getTaskWorkerList(){
        return taskWorkerService.getTaskWorkerList();
    }

    /**
     * 增加一个任务
     * @param taskEntity
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public R save(TaskEntity taskEntity){
        if(taskService.save(taskEntity)>0){
            return R.ok("保存成功");
        }
        return R.error("保存失败");
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model){
        TaskEntity taskEntity = taskService.get(id);
        model.addAttribute("job",taskEntity);
        return "/edit";
    }


    @PostMapping("/update")
    @ResponseBody
    public R update(TaskEntity taskEntity){
        taskService.update(taskEntity);
        return R.ok("更新成功");
    }

    /**
     * 删除一个任务
     * @param id
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public R remove(Long id){
        if(taskService.remove(id)>0){
            return R.ok("成功删除");
        }
        return R.error("删除失败");
    }

    /**
     * 批量删除任务
     * @param ids
     * @return
     */
    @PostMapping("/batchRemote")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Long[] ids){
        taskService.batchRemove(ids);
        return R.ok("批量删除成功");
    }

    @PostMapping("/taskRunOne")
    @ResponseBody
    public R taskRunOne(Long id,String cmd){
        try {
            taskService.runOneById(id);
            return R.ok("任务运行成功");
        } catch (DataTransferException e) {
            LOGGER.error("运行出错",e);
        }
        return R.ok("任务运行失败");
    }


    @PostMapping("/changeJobStatus")
    @ResponseBody
    public R changeJobStatus(Long id,String cmd){
        String label = "停止";
        if("start".equals(cmd)){
            label = "启动";
        }else{
            label = "停止";
        }
        try{
            taskService.changeStatus(id, cmd);
            return R.ok("任务" + label +"成功");
        } catch (DataTransferException e) {
            LOGGER.error("任务改变状态出错",e);
        }
        return R.ok("任务" + label +"失败");
    }



}
